home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Workbench Design
/
WB Collection.iso
/
workbench werkzeuge
/
scherz programme
/
fortune
/
algorithm
next >
Wrap
Text File
|
1996-04-07
|
2KB
|
79 lines
By popular demand, here's a brief explanation of how the fortune program
works. It actually does a lot more than this, with regard to Workbench
stuff, but this is the basics. The actual program is also appallingly
written - it grew organically from a CLI only program.
File format
-----------
huffman compression table (see text.c)
long: number of fortunes (called "num" hereafter).
num longs: start position of each fortune text
num strings:the fortunes. The strings are null-terminated compressed
strings.
Makefort Algorithm
------------------
open the input file
clearhuffman() (initialise the working table)
count=0
do
read a fortune, until you get an EOF or %%
addtohuffman(fortune)
(this analyses the fortune for later compression)
add fortune to linked list of fortunes
increment count
until End of File
close the input file
genhuffman() (generate the compression table)
freehuffman() (free the working table)
open the output file
savehuffman(output file pointer) (save the compression table)
write count as a long integer to the output file
pos=4*(number of fortunes+1)+20*16
(this is the position of the first fortune text.
4*(number of fortunes+1) is the size of the jumptable and
fortune count, and 20*16 is the size of the compression table)
(... and write the jump table)
for each fortune in the linked list do
write pos as a long to the output file
increment pos by the length of the compressed string, +1 for the NULL
(in C: pos+=strlen(compress(fortune->data))+1)
at the end.
end
(finally write the fortunes)
for each fortune in the linked list do
compress the fortune
write it out, followed by a NULL.
end
close the output file
The Readfort Algorithm
----------------------
This is a simple version of the fortune cookie program- it only
reads one fortune, and prints it in the CLI window.
open the input file
loadhuffman(file pointer) (read the compression table)
read the number of fortunes, as a long
fortune chosen=random number from 0 to number of fortunes-1
seek to the position 4*(fortune chosen+1) + 20*16
read a long integer - this is the position of the fortune text
seek to position given by this integer
read a null-terminated string
uncompressed string=uncompress(string read in)
print uncompressed string
Easy, huh?